// matrix_add.c
#include "matrix_operations.h"

void matrix_add(Matrix a, Matrix b, Matrix result) {
    if (a.rows != b.rows || a.cols != b.cols) {
        fprintf(stderr, "Error: Matrices must have the same dimensions.\n");
        return;
    }
    
    // 确保result矩阵已分配内存
    if (result.data == NULL) {
        result.data = (uint8_t*)malloc(a.rows * a.cols * sizeof(uint8_t));
        result.rows = a.rows;
        result.cols = a.cols;
    }

    // 执行加法
    for (int i = 0; i < a.rows * a.cols; i++) {
        result.data[i] = a.data[i] + b.data[i];
    }
}

void add_matrix(int rows, int cols, double mat1[rows][cols], double mat2[rows][cols], double result[rows][cols]) {
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            result[i][j] = mat1[i][j] + mat2[i][j];
        }
    }
}